//var aypId;
var gradeArray;
var gradeId;
var gradeTitle;
var genderArr = new Array({
	genderName : "Male"
}, {
	genderName : "Female"
});
var countryArr = new Array({
	countryName : "India"
});

var religionArr = new Array("Hindu", "Islam", "Christian", "Sikh", "Buddh", "Jain", "Other");
var casteArr = new Array("General", "SC", "ST", "OBC");
var statesArr = new Array({
	stateName : "Andhra Pradesh"
}, {
	stateName : "Arunachal Pradesh"
}, {
	stateName : "Assam"
}, {
	stateName : "Bihar"
}, {
	stateName : "Chhattisgarh"
}, {
	stateName : "Dadra & Nagar Haveli"
}, {
	stateName : "Daman & Diu"
}, {
	stateName : "Goa"
}, {
	stateName : "Gujarat"
}, {
	stateName : "Haryana"
}, {
	stateName : "Himachal Pradesh"
}, {
	stateName : "Jammu and Kashmir"
}, {
	stateName : "Jharkhand"
}, {
	stateName : "Karnataka"
}, {
	stateName : "Kerala"
}, {
	stateName : "Madhya Pradesh"
}, {
	stateName : "Maharashtra"
}, {
	stateName : "Manipur"
}, {
	stateName : "Meghalaya"
}, {
	stateName : "Mizoram"
}, {
	stateName : "Nagaland"
}, {
	stateName : "New Delhi"
}, {
	stateName : "Orissa"
}, {
	stateName : "Punjab"
}, {
	stateName : "Rajasthan"
}, {
	stateName : "Sikkim"
}, {
	stateName : "Tamil Nadu"
}, {
	stateName : "Tripura"
}, {
	stateName : "Uttar Pradesh"
}, {
	stateName : "Uttarakhand"
}, {
	stateName : "West Bengal"
});

var bloodGrArr = new Array({
	bloodGrName : "O+"
}, {
	bloodGrName : "O-"
}, {
	bloodGrName : "A+"
}, {
	bloodGrName : "A-"
}, {
	bloodGrName : "B+"
}, {
	bloodGrName : "AB+"
}, {
	bloodGrName : "AB-"
}, {
	bloodGrName : 'Not Known'
});

var studentsDataViewModel;

var studentsDataModel = function ViewModel(data) {
	//console.log("curriculam view model");
	var self = this;
	self.studentsInfo = ko.observable();
	self.parentsInfo = ko.observable();
	self.motherInfo = ko.observable();
	self.guardiansInfo = ko.observable();
	self.guardianArr = ko.observableArray(["Father", "Mother", "Other"]);
	self.guardianValue = ko.observable().extend({
		required : true
	});
	self.update(data);
}

studentsDataModel.prototype.update = function(data) {
	var self = this;
	console.log("update data", data);
	
	if(data == undefined)
	{
		data="";
	}
	if (data == "") {
		console.log("--->First");
		self.studentsInfo(new studentDetails(""));
		self.parentsInfo(new parentDetails(""));
		self.motherInfo(new motherDetails(""));
		self.guardiansInfo(new guardianDetails(""));
	}
	else{
		console.log("------>data:",data);
		self.studentsInfo(new studentDetails(data));
		self.parentsInfo(new parentDetails(data.parents[0]));
		self.motherInfo(new parentDetails(data.parents[1]));
		self.guardiansInfo(new guardianDetails(data.guardian));
		self.guardianValue(data.guardian.relationWithStudent);
		
		jQuery("#saveInfo").hide();
		jQuery("#cencelaBox").text('Close');
		
		for (var i = 0; i < self.studentsInfo().gradeArr().length; i++) {
		if (data.classId == self.studentsInfo().gradeArr()[i].classId) {
			self.studentsInfo().grade(self.studentsInfo().gradeArr()[i]);
			console.log("inside for loop", self.studentsInfo().gradeArr()[i]);
			break;
		}
	}
	
	//	self.studentsInfo(new studentDetails(data.studentsInfo));
	//	self.parentsInfo(new parentDetails(data.parentsInfo));
	//	self.motherInfo(new parentDetails(data.motherInfo));
	//	self.guardiansInfo(new guardianDetails(data.guardiansInfo));
		//self.parentsInfo().copyData((data.guardiansInfo.relationWithStudent).toLowerCase());
		////console.log("data.guardiansInfo.relationWithStudent",data.guardiansInfo);
	}

	//	var className = grade;
	//	self.studentsInfo().aypId(aypIdGlobal);
	//	self.studentsInfo().gradeArr(gradeArray);
	//	self.studentsInfo().classId(classIdGlobal);
	/*
	 for (var i = 0; i < self.studentsInfo().gradeArr().length; i++) {
	 if (grade.classId == self.studentsInfo().gradeArr()[i].classId) {
	 self.studentsInfo().grade(self.studentsInfo().gradeArr()[i]);
	 console.log("inside for loop", self.studentsInfo().gradeArr()[i]);
	 break;
	 }
	 }
	 */
	self.parentsInfo().copyInfo = ko.computed(function() {
		console.log("guardianValue:", self.guardianValue());

		var fdata = self.parentsInfo();
		var mdata = self.motherInfo();
		var gdata = self.guardiansInfo();

		if (self.guardianValue() == "Father") {
			self.guardiansInfo().firstName(fdata.firstName());
			self.guardiansInfo().middleName(fdata.middleName());
			self.guardiansInfo().lastName(fdata.lastName());
			self.guardiansInfo().mobileNumber(fdata.mobileNumber());
			self.guardiansInfo().emailAddress(fdata.emailAddress());
			self.guardiansInfo().occupation(fdata.occupation());
			self.guardiansInfo().qualification(fdata.qualification());
			self.guardiansInfo().annualIncome(fdata.annualIncome());
			self.guardiansInfo().bloodGrp(fdata.bloodGrp());
			self.guardiansInfo().relationWithStudent("Father");
		} else if (self.guardianValue() == "Mother") {
			self.guardiansInfo().firstName(mdata.firstName());
			self.guardiansInfo().middleName(fdata.middleName());
			self.guardiansInfo().lastName(mdata.lastName());
			self.guardiansInfo().mobileNumber(mdata.mobileNumber());
			self.guardiansInfo().emailAddress(mdata.emailAddress());
			self.guardiansInfo().occupation(mdata.occupation());
			self.guardiansInfo().qualification(mdata.qualification());
			self.guardiansInfo().annualIncome(mdata.annualIncome());
			self.guardiansInfo().bloodGrp(mdata.bloodGrp());
			self.guardiansInfo().relationWithStudent("Mother");
		} else {
			self.guardiansInfo().firstName(gdata.firstName());
			self.guardiansInfo().middleName(gdata.middleName());
			self.guardiansInfo().lastName(gdata.lastName());
			self.guardiansInfo().mobileNumber(gdata.mobileNumber());
			self.guardiansInfo().emailAddress(gdata.emailAddress());
			self.guardiansInfo().occupation(gdata.occupation());
			self.guardiansInfo().qualification(gdata.qualification());
			self.guardiansInfo().annualIncome(gdata.annualIncome());
			self.guardiansInfo().bloodGrp(gdata.bloodGrp());
			self.guardiansInfo().relationWithStudent(gdata.relationWithStudent());
		}

		self.studentsInfo().parents.removeAll();
		self.studentsInfo().parents.push(fdata, mdata);
		self.studentsInfo().guardian(self.guardiansInfo());
		//savedData = self.studentsInfo();
		//console.log(savedData);
		return name;
	});

	self.studentsInfo().lastName.subscribe(function(newValue) {
		console.log("------> lastname changed");
		self.parentsInfo().lastName(newValue);
		self.motherInfo().lastName(newValue);
	})
	//console.log(savedData);
	self.saveStudentHandler = function() {
		console.log("saving student details");
		var vm = ko.validation.group(this);

		if (this.isValid() == false) {
			vm.showAllMessages();
			alert("Please fill all Required fields before saving.");
			return;
		}

		console.log("::::::::", this, self, studentsDataViewModel);
		console.log("VM:", vm(), this.isValid());

		var mapping = {
			'ignore' : ["bloodGrArr", "stateArr", "countryArr", "divisionArr", "genderArr", "religionArr", "casteArr", "gradeArr", "parentsInfo", "motherInfo", "copyData", "copyInfo", "fullName", "guardiansInfo", "grade", "division", "enbleDropDown", "guardianArr", "guardianValue"]
		}
		var jsonData = ko.mapping.toJSON(self.studentsInfo(), mapping);
		console.log("data for server", jsonData);
		
		if (jQuery('#saveInfo').hasClass('disabled')) return;
		
		saveStudentsData(jsonData);
	}

	self.cancelHandler = function() {
		console.log("------> cancel student details");

	/*	self.studentsInfo(new studentDetails(""));
		self.parentsInfo(new parentDetails(""));
		self.motherInfo(new motherDetails(""));
		self.guardiansInfo(new guardianDetails(""));*/
		studentsDataViewModel.update("");
		jQuery("#dialog-student").dialog("close");
		
		jQuery("#saveInfo").show();
		jQuery("#cencelaBox").text('Cancel');
	}
};


function minimizeHandler(event) {
	console.log("-------> minimizeHandler");

	jQuery("#rad_buttons span:first").removeClass("active");
	jQuery("#rad_buttons span:last").removeClass("active");
	jQuery(event.toElement).addClass("active");

	jQuery('.not_required').hide();

	saveUserBookMark("minimize");
}

function normalizeHandler(event) {
	console.log("-------> normalizeHandler");

	jQuery("#rad_buttons span:first").removeClass("active");
	jQuery("#rad_buttons span:last").removeClass("active");
	jQuery(event.toElement).addClass("active");

	jQuery('.not_required').show();

	saveUserBookMark("normal");
}

function getUserBookMark() {
	console.log("------> getting user book mark");

	jQuery.ajax({
		data : {
			type : "addStudentForm"
		},
		url : stmgUrl + "getUserBookmark",
		type : "post",
		success : function(result) {
			console.log("result:", result);
			if (result == null || result.data == "normal") {
				jQuery("#rad_buttons span:first").removeClass("active");
				jQuery("#rad_buttons span:last").removeClass("active");
				jQuery("#rad_buttons span:first").addClass("active");

				jQuery('.not_required').show();
			} else {
				jQuery("#rad_buttons span:first").removeClass("active");
				jQuery("#rad_buttons span:last").removeClass("active");
				jQuery("#rad_buttons span:last").addClass("active");

				jQuery('.not_required').hide();
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			//console.log("error");
		}
	});

}

function saveUserBookMark(data) {
	console.log("------> saving user book mark");

	jQuery.ajax({
		data : {
			data : data,
			type : "addStudentForm"
		},
		url : stmgUrl + "saveUserBookmark",
		type : "post",
		success : function(result) {
			console.log("result:", result);
		},
		error : function(jqXHR, textStatus, errorThrown) {
			//console.log("error");
		}
	});
}

function saveStudentsData(savedata) {
	console.log("----> save data to server", savedata, stmgUrl);
	
	jQuery('#saveInfo').addClass('disabled');
	
	jQuery.ajax({
		url : stmgUrl + "saveStudent",
		type : "post",
		data : {
			savedata : savedata
		},
		success : function(result) {
			console.log("result", result);
			ko.utils.arrayForEach(gradeListViewModel.gradeList(), function(item) {
				console.log("------>Item",item,gradeListViewModel);
			
				ko.utils.arrayForEach(item.divisions(), function(division) {
					//console.log("#------------>", result.data[i].ceGradeId, item.ceGradeId());
					//console.log("##------------>", result.data[i].classId, division.classId());
					if (result.classId == division.classId()) {

					/*	if (result.gender == "Male") {
							console.log("---->Male");
							division.maleCount(division.maleCount() + 1);
							division.totalCount(division.totalCount() + 1);
						} else {
							console.log("---->FeMale");
							division.femaleCount(division.femaleCount() + 1);
							division.totalCount(division.totalCount() + 1);
						}
						*/
						//console.log("------------>", division);
						var studentViewModel = new studentModel();
						studentViewModel.userStudentId(result.userStudentId);
						studentViewModel.firstName(result.firstName);
						studentViewModel.lastName(result.lastName);
						studentViewModel.gender(result.gender);
						studentViewModel.classId(result.classId);
						studentViewModel.rollNumber(result.rollNumber == 0 ? "Un Assigned" : result.rollNumber);
						
						var simpleDiv = new simpleDivisionModel();
						simpleDiv.isAssigned('true');
						simpleDiv.divisionTitle(division.divisionTitle());
						simpleDiv.classId(division.classId());
						
						studentViewModel.selectedDivision(simpleDiv);
						
					//	studentViewModel.isAssigned('true');
						console.log("result[i].rollNumber:", result.rollNumber)
						//var roll_Number = result[i].rollNumber==0?"UnAssigned":result[i].rollNumber;

						item.studentList.push(studentViewModel);
						item.totalCount(item.totalCount()+1);
						
						console.log("studentListViewModel:",studentListViewModel);
						console.log("item:",item);
						
					}

					division.dashBoardSexRatio(division.sexRatio());
					division.dashBoardTotalCount(division.totalCount());

					//division.dashBoardStudentList = ko.observable(division.sexRatio());

				});
			});
			studentsDataViewModel.update("");
			
		/*	studentsDataViewModel.studentsInfo(new studentDetails(""));
			studentsDataViewModel.parentsInfo(new parentDetails(""));
			studentsDataViewModel.motherInfo(new motherDetails(""));
			studentsDataViewModel.guardiansInfo(new guardianDetails(""));
		*/
			jQuery('#saveInfo').removeClass('disabled');
			jQuery("#dialog-student").dialog("close");
		},
		error : function(jqXHR, textStatus, errorThrown) {
			jQuery("#dialog-student").dialog("close");
			studentsDataViewModel.update("");
		/*	studentsDataViewModel.studentsInfo(new studentDetails(""));
			studentsDataViewModel.parentsInfo(new parentDetails(""));
			studentsDataViewModel.motherInfo(new motherDetails(""));
			studentsDataViewModel.guardiansInfo(new guardianDetails(""));
			*/
			console.log("error");
		}
	});
}

function saveStudentSuccessHandler(result) {

	console.log("result", result);
	jQuery("#dialog-student").dialog("close");

	/*ko.utils.arrayForEach(gradeListViewModel.gradeList(), function(item) {

	 ko.utils.arrayForEach(item.divisions(), function(division) {
	 for (var i = 0; i < result.data.length; i++) {
	 //console.log("#------------>", result.data[i].ceGradeId, item.ceGradeId());
	 //console.log("##------------>", result.data[i].classId, division.classId());
	 if ((result.data[i].ceGradeId == item.ceGradeId()) && (result.data[i].classId == division.classId())) {
	 //console.log("------------>", division);
	 division.femaleCount(result.data[i].femaleCount);
	 division.maleCount(result.data[i].maleCount);
	 division.totalCount(result.data[i].totalCount);
	 division.isRollNumAssigned(result.data[i].isRollNumAssigned);
	 }
	 }

	 division.dashBoardSexRatio(division.sexRatio());
	 division.dashBoardTotalCount(division.totalCount());

	 //division.dashBoardStudentList = ko.observable(division.sexRatio());

	 });
	 });
	 */
	
	studentsDataViewModel.update("");
	
/*	studentsDataViewModel.studentsInfo(new studentDetails(""));
	studentsDataViewModel.parentsInfo(new parentDetails(""));
	studentsDataViewModel.motherInfo(new motherDetails(""));
	studentsDataViewModel.guardiansInfo(new guardianDetails(""));
*/
	console.log("succesfully saved", event);
}

var studentDetails = function viewStudentsDetail(data) {
	//alert("viewM called");

	var self = this;
	self.firstName = ko.observable("").extend({
		required : true
	});
	self.lastName = ko.observable("").extend({
		required : true
	});
	self.middleName = ko.observable("");
	self.rollNumber = ko.observable();
	self.gender = ko.observable().extend({
		required : true
	});
	self.dob = ko.observable();
	self.nationality = ko.observable();
	self.genderArr = ko.observableArray(genderArr);
	self.religionArr = ko.observableArray(religionArr);
	self.casteArr = ko.observableArray(casteArr);
	console.log("gradeArray::::::::", gradeArray);
	self.grade = ko.observable().extend({
		required : true
	});
	;
	self.division = ko.observable();
	self.aypId = ko.observable();
	self.classId = ko.observable();
	self.crsId = ko.observable();
	self.dateOfJoin = ko.observable(convertDateToString(new Date(), "/"));
	self.lastSchoolAttended = ko.observable();
	self.aadharCardId = ko.observable();

	self.email = ko.observable();
	self.addressOne = ko.observable("").extend({
		required : true
	});
	self.addressTwo = ko.observable();
	self.city = ko.observable();
	self.pinCode = ko.observable();
	self.state = ko.observable();
	self.country = ko.observable();
	self.phone = ko.observable("").extend({
		required : true,
		number : true
	});
	self.userStudentId = ko.observable();
	self.religion = ko.observable().extend({
		required : true
	});
	self.caste = ko.observable().extend({
		required : true
	});
	self.casteCategory = ko.observable();
	self.motherTongue = ko.observable();

	self.bloodGrp = ko.observable().extend({
		required : true
	});
	self.diseases = ko.observable();
	self.grNo = ko.observable("").extend({
		required : true
	});
	self.commutesBy = ko.observable();
	self.fullName = ko.observable();
	self.gradeArr = ko.observable(gradeArray);
	self.divisionArr = ko.observable();
	self.bloodGrArr = ko.observable(bloodGrArr);

	self.countryArr = ko.observable(countryArr);
	self.stateArr = ko.observable(statesArr);
	self.parents = ko.observableArray();
	self.guardian = ko.observable();
	self.enbleDropDown = ko.observable();
	self.homeTown = ko.observable();
	self.grade.subscribe(function(newValue) {
		console.log("gradeChange:", newValue);

		if (newValue == undefined) {
			return;
		}
		classChanged = true;
		//	self.aypId(aypIdGlobal);

		self.classId(newValue.classId);

	});

	self.update(data);
}


studentDetails.prototype.update = function(data) {
	console.log("------->data", data);
	
	if(data == undefined)
	{
		data="";
	}
	
	var self = this;

	self.firstName(data.firstName || "");
	self.lastName(data.lastName || "");
	self.middleName(data.middleName || "");

	self.rollNumber(data.rollNumber || 0);
	self.gender(data.gender || "");
	self.dob(data.dob || "");
	self.nationality(data.nationality || "");
	self.genderArr(genderArr);

	self.religionArr(religionArr);
	self.casteArr(casteArr);

	self.grade(data.grade || "");
	console.log("===============****========", self.grade());
	self.division(data.division || "");

	self.aypId(data.aypId || "");
	self.classId(data.classId || "");
	self.crsId(data.crsId || "");

	self.dateOfJoin(data.dateOfJoin || convertDateToString(new Date(), "/"));
	self.lastSchoolAttended(data.lastSchoolAttended || "");
	self.aadharCardId(data.aadharCardId || "");

	self.email(data.email || "");
	self.addressOne(data.addressOne || "");
	self.addressTwo(data.addressTwo || "");
	self.city(data.city || "");
	self.pinCode(data.pinCode || "");
	self.state(data.state || "");
	self.country(data.country || "");
	self.phone(data.phone || "");
	self.userStudentId(data.userStudentId || "");

	self.religion(data.religion || "");
	self.caste(data.caste || "");
	self.casteCategory(data.casteCategory || "");
	self.motherTongue(data.motherTongue || "");
	self.homeTown(data.homeTown || "");

	self.bloodGrp(data.bloodGrp || "");
	self.diseases(data.diseases || "");
	self.grNo(data.grNo || "");
	self.commutesBy(data.commutesBy || "");
	self.enbleDropDown(data.enbleDropDown || "");
	self.divisionArr([{
		divisionName : "A"
	}, {
		divisionName : "B"
	}, {
		divisionName : "C"
	}, {
		divisionName : "D"
	}, {
		divisionName : "E"
	}]);
	self.bloodGrArr(bloodGrArr);

	self.countryArr(countryArr);
	self.stateArr(statesArr);

	self.fullName = ko.computed(function() {
		
		if (self.gender() == "Male") {
			return "Mst. " + self.firstName() + " " + self.lastName();
		} else if (self.gender() == "Female") {
			return "Ms. " + self.firstName() + " " + self.lastName();
		}
	});

	console.log("self:", self);
}

var parentDetails = function viewParentsDetail(data) {
	//alert("viewM called");
	var self = this;
	self.firstName = ko.observable("").extend({
		required : true
	});
	self.lastName = ko.observable("").extend({
		required : true
	});
	self.middleName = ko.observable("");
	self.occupation = ko.observable();
	self.qualification = ko.observable();
	self.mobileNumber = ko.observable("").extend({
		required : true,
		number : true
	});
	self.emailAddress = ko.observable();

	self.bloodGrp = ko.observable();
	self.annualIncome = ko.observable();

	self.copyData = ko.observable();

	self.bloodGrArr = ko.observable(bloodGrArr);
	self.update(data);
}

parentDetails.prototype.update = function(data) {

	var self = this;
	self.firstName(data.firstName || "");
	self.lastName(data.lastName || "");
	self.middleName(data.middleName || "");

	self.occupation(data.occupation || "");
	self.qualification(data.qualification || "");
	self.mobileNumber(data.mobileNumber || "");
	self.emailAddress(data.emailAddress || "");

	self.bloodGrp(data.bloodGrp || "");
	self.annualIncome(data.annualIncome || "");

	//self.copyData(data.copyData || "mother");

	self.bloodGrArr(bloodGrArr);
}

var motherDetails = function viewMotherDetail(data) {
	//alert("viewM called");
	var self = this;
	self.firstName = ko.observable("").extend({
		required : true
	});
	self.lastName = ko.observable("").extend({
		required : true
	});
	self.middleName = ko.observable("");
	self.occupation = ko.observable();
	self.qualification = ko.observable();
	self.mobileNumber = ko.observable("").extend({
		required : true,
		number : true
	});
	self.emailAddress = ko.observable();

	self.bloodGrp = ko.observable();
	self.annualIncome = ko.observable();

	//self.copyData = ko.observable();

	self.bloodGrArr = ko.observable(bloodGrArr);
	self.update(data);
}

motherDetails.prototype.update = function(data) {

	var self = this;
	self.firstName(data.firstName || "");
	self.lastName(data.lastName || "");
	self.middleName(data.middleName || "");

	self.occupation(data.occupation || "");
	self.qualification(data.qualification || "");
	self.mobileNumber(data.mobileNumber || "");
	self.emailAddress(data.emailAddress || "");

	self.bloodGrp(data.bloodGrp || "");
	self.annualIncome(data.annualIncome || "");

	//self.copyData(data.copyData || "other");
	self.bloodGrArr(bloodGrArr);
}

var guardianDetails = function viewGuardiansDetail(data) {
	//alert("viewM called");
	var self = this;
	self.firstName = ko.observable("").extend({
		required : true
	});
	self.lastName = ko.observable("").extend({
		required : true
	});
	self.relationWithStudent = ko.observable();
	self.middleName = ko.observable();
	self.occupation = ko.observable();
	self.qualification = ko.observable();
	self.mobileNumber = ko.observable("").extend({
		required : true,
		number : true,

	});
	self.emailAddress = ko.observable();

	self.bloodGrp = ko.observable();
	self.annualIncome = ko.observable();

	self.bloodGrArr = ko.observable(bloodGrArr);
	self.update(data);
}

guardianDetails.prototype.update = function(data) {

	var self = this;
	self.firstName(data.firstName || "");
	self.lastName(data.lastName || "");
	self.middleName(data.middleName || "");
	self.relationWithStudent = ko.observable(data.relationWithStudent || "");

	self.occupation(data.occupation || "");
	self.qualification(data.qualification || "");
	self.mobileNumber(data.mobileNumber || "");
	self.emailAddress(data.emailAddress || "");

	self.bloodGrp(data.bloodGrp || "");
	self.annualIncome(data.annualIncome || "");

	self.bloodGrArr(bloodGrArr);

}

ko.validation.init({
	grouping : {
		deep : true,
		observable : true
	}
});

/**
 * Will Converts Date to String based on Type
 *
 */
function convertDateToString(val, type) {

	type = type == undefined ? "/" : type;

	console.log("convertDateToString:", ('0' + val.getDate()).slice(-2) + '/' + ('0' + (val.getMonth() + 1)).slice(-2) + '/' + val.getFullYear())
	if (type == "-") {
		return val.getFullYear() + '-' + ('0' + (val.getMonth() + 1)).slice(-2) + '-' + ('0' + val.getDate()).slice(-2);
	} else if (type == "/") {
		return ('0' + val.getDate()).slice(-2) + '/' + ('0' + (val.getMonth() + 1)).slice(-2) + '/' + val.getFullYear();
	}
}

function getAcademicClasses(aypId, sectionId) {
	console.log("----> Initial data from server", aypId, stmgUrl);

	jQuery.ajax({
		url : stmgUrl + "getAcademicClasses",
		type : "post",
		data : {
			aypId : aypId,
			sectionId : sectionId
		},
		success : function(result) {
			console.log("Classes:", result);
			gradeArray = [];
			for (var i = 0; i < result.length; i++) {

				var obj = result[i];
				obj.gradeName = result[i].gradeTitle + " " + result[i].divisionTitle;
				gradeArray.push(obj);
			}

			studentsDataViewModel = new studentsDataModel("");
			console.log("studentsDataViewModel:::", studentsDataViewModel);
			ko.applyBindings(studentsDataViewModel, document.getElementById("dialog-student"));
			//	initialStudentLoading = true;

		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("error");
		}
	});
}

jQuery(function() {

	jQuery("#shifts").buttonset();

	aypId = jQuery('#aypId').val();
	academicYear(jQuery('#aypTitle').val());
	console.log("#aypId:", aypId);
	console.log("Document Ready", aypId);

	jQuery("#dobDatepicker").datepicker("option", "maxDate", new Date());
	jQuery("#dobDatepicker").datepicker("option", "minDate", "-20y");
	jQuery("#dobDatepicker").datepicker("option", "yearRange", "-20:+01");

	getAcademicClasses(aypId, sectionId);
	getUserBookMark();

});
